<!--{include file='admin/public/header.html'}-->
<script>
var index = <!--{$index}-->;
$(function(){
	$('.navtype').unbind().bind('change',function(){getNavCate(this)});
	$('.cate').unbind().bind('change',function(){getNavName(this)});
})

function getNavCate(obj){
	var html = '';
	$.ajax({
		async:false, 
		url:'<!--{U str="admin/setting/getCate"}-->',
		type:'get',
		data:{type:obj.value},
		success:function(data){
			if(data.result){
				html += "<select name='cate[]' class='cate'>";
				html += "<option>请选择...</option>"
				for(i in data.data){
					if(data.data[i]['parentid'] == 0){
						html += "<option value='"+data.data[i]['id']+"'><b>"+data.data[i]['name']+"<b></option>";
					}else{
						html += "<option value='"+data.data[i]['id']+"'>&nbsp;&nbsp;"+data.data[i]['name']+"</option>";
					}
					
				}
				html += "</select>";
			}else{
				$.dialog.alert(data.data.reason);
			}
		}
	});
	$(obj).parents().parents().children('.navcate').html(html);
	$('.cate').unbind().bind('change',function(){getNavName(this)});
}

function addNav(obj){
	var tr = $(".table.set_table tbody tr").eq(0).clone(); 
	index = parseInt(index)+1;
	tr.attr('class', 'nav'+index);
	tr.find(".navtype").find("option:selected").attr("selected",false);
	tr.find(".cate").html('<option>请选择...</option>');
	tr.find(".navname").attr('value','');
	$('.table.set_table').children(":last").append(tr);
	$('.navtype').unbind().bind('change',function(){getNavCate(this)});
}

function getNavName(obj){
	$(obj).parents().parents().children('.navname').find("input[name='name[]']").attr('value',$(obj).find("option:selected").text());
}

function delNav(obj){
	if($(".table.set_table tbody tr").length > 1){
		var classIndex = '.';
		classIndex += $(obj).parents().parents().attr('class');
		$(classIndex).remove();
	}else{
		$.dialog.alert('导航栏至少需要一项!');
	}
}
//上下移动
function move(obj,type){
	var key = $(obj).parents().parents().attr('class');
	var i = moveNav =  '';
	if(type == '+'){
		i = parseInt(key.replace('nav',''))-1;
	}else if(type == '-'){
		i = parseInt(key.replace('nav',''))+1;
	}
	
	var moveNav = '.nav'+i;
	var nowNav = '.'+key;
	 
	var nowNavHtml = $(nowNav).html();
	var prevNavHtml = $(moveNav).html();
	$(nowNav).html(prevNavHtml);
	$(moveNav).html(nowNavHtml)
}
</script>
<form name="form1" id="form1" action="" method="post">
<div class="content">
        <div class="page-header">
          <h3 class="fl">头部导航设置</h3>  
          <div class="user_message fr"><i class="icon-wrench"></i>网站设置</div>
          <div class="cl"></div>
        </div>
        <table class="table set_table">
        <thead>
            <tr>
              <th width="11%" >模块</th>
              <th width="30%">分类</th>
              <th width="20%" class="" >名称</th>
              <th class="" >操作</th>
            </tr>
         </thead>
            <tbody>
            <!--{if empty($headerNav)}-->
            <tr class="nav0">
              <td>
              	<select name='type[]' class='navtype'>
              	<option>请选择...</option>
              	<!--{foreach from=$type key=k item=v}-->
              	<option value='<!--{$k}-->'><!--{$v}--></option>
              	<!--{/foreach}-->
              	</select>
              </td>
              <td class="navcate"></td>
              <td class="navname"><input name="name[]" type="text" /></td>
              <td>
              <a href="javascript:;" onclick="addNav(this)">增加</a>
              <a href="javascript:;" onclick="delNav(this)">删除</a>
              </td>
            </tr>
           <!--{else}-->
            <!--{foreach from=$headerNav.value key=k item=v}-->
            <tr class="nav<!--{$k}-->">
              <td>
              	<select name='type[]' class='navtype'>
              	<option>请选择...</option>
              	<!--{foreach from=$type key=ik item=iv}-->
              	<option value='<!--{$ik}-->' <!--{if $ik == $v.type}--> selected="selected" <!--{/if}-->><!--{$iv}--></option>
              	<!--{/foreach}-->
              	</select>
              </td>
              <td class="navcate">
              	<select name='cate[]' class='cate'>
              	<option>请选择...</option>
              	<!--{foreach from=$v.cates key=iik item=iiv}-->
              	<!--{if $iiv.parentid == 0}-->
              		<option value='<!--{$iiv.id}-->' <!--{if $iiv.id == $v.id}--> selected="selected" <!--{/if}-->><!--{$iiv.name}--></option>
              	<!--{else}-->
	              	<option value='<!--{$iiv.id}-->' <!--{if $iiv.id == $v.id}--> selected="selected" <!--{/if}-->>&nbsp;&nbsp;<!--{$iiv.name}--></option>
              	<!--{/if}-->
              	<!--{/foreach}-->
              	</select>
              </td>
              <td class="navname"><input class="navname" name="name[]" type="text" value="<!--{$v.name}-->"/></td>
              <td>
              <a href="javascript:;" onclick="addNav(this)">增加</a>
              <a href="javascript:;" onclick="delNav(this)">删除</a>
              <a href="javascript:;" onclick="move(this,'+')">上移</a>
              <a href="javascript:;" onclick="move(this,'-')">下移</a>
              </td>
            </tr>
            <!--{/foreach}-->
            <!--{/if}-->
            </tbody>
        </table>
        <p class="border_top"> <button class="btn btn-primary btn-small"  type="submit" >提交</button> </p>
        <div class="copyright"></div>
</div>
</form>

<!--{include file='admin/public/footer.html'}-->